﻿// -----------------------------------------------------------------------
// <copyright file="Utitiy.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace BigStamp.Entity
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Windows;
    using System.IO;
    using BigStamp.Entity;
    using System.Printing;
    using System.Windows.Documents;
    using System.Windows.Controls;
    using System.Drawing;
    using System.Drawing.Printing;
    using System.Windows.Data;
    

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class Utility
    {
        static readonly string DATABASEPATH = System.Environment.CurrentDirectory;
        public static Database BigStampDatabase=new Database();
        public static OrderForm FormToPrint = new OrderForm();

        //public static ICollectionView view = CollectionViewSource.GetDefaultView(BigStampDatabase.Forms);

        public static ICollectionView GetForms() 
        {
            return CollectionViewSource.GetDefaultView(BigStampDatabase.Forms);
        }
        

        static Utility()
        {
           LoadDatabase(DATABASEPATH + "\\BigStamp.db");
            SaveToDatabse(DATABASEPATH + @"\" + DateTime.Now.ToShortDateString().Replace("/", "-") + "BigStamp.db");

        }


        public static void SaveToDatabse()
        {
            SaveToDatabse(DATABASEPATH + "\\BigStamp.db");
        }

        public static void SaveToDatabse(string dbPath)
        {
            BinaryFormatter bf = new BinaryFormatter();
            using (FileStream fs = new FileStream(dbPath, FileMode.Create))
            {
                bf.Serialize(fs, BigStampDatabase);
            }
        }

        public static void AddUser(string screenName, string address, string tel)
        {
            BigStampDatabase.Users.Add(new User(screenName, address, tel));
        }

        public static void LoadDatabase(string dbPath)
        {
            if (File.Exists(dbPath))
            {
                BinaryFormatter bf = new BinaryFormatter();
                using (FileStream fs = new FileStream(dbPath, FileMode.Open))
                {
                    BigStampDatabase = (Database)bf.Deserialize(fs);
                }
            }
        }

        public static void AddForm(string destCity, string destAddr, string srcName, string srcAddr,string srcTel ,string destName, string destTel, string destTitle, string DestZipCode, string formNum) 
        {
            OrderForm form = new OrderForm();
            form.DestCity=destCity;
            form.CreateDate=DateTime.Now;
            form.DestAddr = destAddr;
            form.DestName = destName;
            form.SrcAddr = srcAddr;
            form.SrcName = srcName;
            form.SrcTel = srcTel;
            form.DestTel = destTel;
            form.DestTitle = destTitle;
            form.FormNum = formNum;
            form.DestZipCode = DestZipCode;
            form.CreateDate = DateTime.Now;

            Utility.BigStampDatabase.Forms.Add(form);
        }

        public static void PrintForm() 
        {
            PrintDialog printDlg = new PrintDialog();
            PrintDocument printDoc=new PrintDocument();

            printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);

            printDoc.Print();


        }

        static void printDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            Font printFont=new Font("黑体",12);
            Font printFontBigger = new Font("黑体", 18);
            //Font printFontZipCode =new Font("黑体",18,
            PointF printOffest = new PointF();


            e.Graphics.DrawString(FormToPrint.SrcName, printFont, Brushes.Black, printOffest.X+75,printOffest.Y+ 0);
            e.Graphics.DrawString(FormToPrint.SrcAddr, printFontBigger, Brushes.Black, printOffest.X + 65, printOffest.Y + 55);
            e.Graphics.DrawString("订单号: "+FormToPrint.FormNum, printFontBigger, Brushes.Black, printOffest.X + 25, printOffest.Y + 100);
            e.Graphics.DrawString(FormToPrint.SrcTel, printFont, Brushes.Black, printOffest.X + 120, printOffest.Y + 140);

            e.Graphics.DrawString(FormToPrint.DestCity, printFont, Brushes.Black, printOffest.X + 450, printOffest.Y+0);
            e.Graphics.DrawString(FormToPrint.DestTel, printFont, Brushes.Black, printOffest.X + 650, printOffest.Y+0);
            e.Graphics.DrawString(FormToPrint.DestTitle, printFont, Brushes.Black, printOffest.X + 450, printOffest.Y+25);
            e.Graphics.DrawString(FormToPrint.DestAddr, printFontBigger, Brushes.Black, printOffest.X + 450, printOffest.Y + 55);
            e.Graphics.DrawString(FormToPrint.DestName, printFont, Brushes.Black, printOffest.X + 450, printOffest.Y+140);
            e.Graphics.DrawString(FormToPrint.DestZipCode, printFontBigger, Brushes.Black, printOffest.X + 640, printOffest.Y + 130);

            //throw new NotImplementedException();
        }
    }
}
